//
// Created by suzhen on 18-1-16.
//

#include <hacks/net/ip_addr.h>

namespace hacks::net {

    //=====================  ip =======================
    ip_addr::~ip_addr() {}

    ip_addr::ip_addr(const std::string &s_addr_, uint16_t port_)
            : s_addr_(s_addr_), port_(port_) {}

    std::uint16_t ip_addr::get_port() const {
        return port_;
    }

    const std::string &ip_addr::get_addr() const {
        return s_addr_;
    }


    //=====================ip v4=======================


    ip4_addr::ip4_addr(const std::string &s_addr_, uint16_t port_)
            : ip_addr(s_addr_, port_),
              sock_in4() {
        bzero(&sock_in4, sizeof sock_in4);
        sock_in4.sin_family = PF_INET;
        sock_in4.sin_port = htons(port_);
        sock_in4.sin_addr.s_addr = inet_addr(s_addr_.c_str());
    }

    sa_family_t ip4_addr::get_domain() const {
        return sock_in4.sin_family;
    }

    //=====================ip v6=======================
    ip6_addr::ip6_addr(const std::string &s_addr_, uint16_t port_)
            : ip_addr(s_addr_, port_),
              sock_in6() {
        bzero(&sock_in6, sizeof sock_in6);
        sock_in6.sin6_family = PF_INET6;
        sock_in6.sin6_port = htons(port_);
        inet_pton(AF_INET6, s_addr_.c_str(), &sock_in6.sin6_addr);
    }

    sa_family_t ip6_addr::get_domain() const {
        return sock_in6.sin6_family;
    }


}